package edu.hit.crawler.util;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Writable;

/**
 * @author 
 */
public class WebContentWritable implements Writable {
    private String content = new String();
    private String url = new String();
    private String charset = new String();

    /**
     * construct 
     */
    public WebContentWritable(String url, String charset, String content) {
		this.url = url;			//存放网页对应的URL地址
		this.charset = charset;	 //存放对应网页的字符编码
		this.content = content;  // 存放网页的字符串对象
    }

    /**
     * default construct
     */
    public WebContentWritable() {
    }

    public WebContentWritable(WebContentWritable ww) {
      this.url = ww.getUrl();
      this.charset = ww.getCharset();
      this.content = ww.getContent();
    }
    
    /**
     * @return
     */
    public String getContent() {
    	return content;
    }

    /**
     * @return
     */
    public String getUrl() {
    	return url;
    }

    /**
     * @return
     */
    public String getCharset() {
    	return charset;
    }

    @Override
    public void readFields(DataInput in) throws IOException {

	url = in.readUTF();
	charset = in.readUTF();
	//content = in.readUTF();
	int len = in.readInt();
    byte[] byteBuffer = new byte[len];
    in.readFully(byteBuffer);
    content = new String(byteBuffer , "utf-8");
    }

    @Override
    public void write(DataOutput out) throws IOException {
	
	out.writeUTF(url);
	out.writeUTF(charset);
	//out.writeUTF(content);
	byte[] byteBuffer = content.getBytes("utf-8");
	out.writeInt(byteBuffer.length);
	out.write(byteBuffer);
    }
}
